package com.rp.admin.serivce.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.rp.admin.mapper.AdminUserMapper;
import com.rp.admin.serivce.AdminUserService;
import com.rp.bo.NewAdminBO;
import com.rp.news.common.PagedResult;
import com.rp.news.util.CommonUtil;
import com.rp.pojo.AdminUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    private AdminUserMapper adminUserMapper;

    @Autowired
    private CommonUtil commonUtil;

    /**
     * 根据用户名查询用户信息
     *
     * @param username
     * @return
     */
    @Override
    public AdminUser queryAdminUserByUsername(String username) {
        Example adminExample = new Example(AdminUser.class);
        Example.Criteria criteria = adminExample.createCriteria();
        criteria.andEqualTo("username", username);
        AdminUser adminUser = adminUserMapper.selectOneByExample(adminExample);
        return adminUser;
    }

    @Override
    public void createAdminUser(NewAdminBO newAdminBO) {
        AdminUser adminuser = Convert.convert(AdminUser.class, newAdminBO);
        String s = IdUtil.fastSimpleUUID();
        adminuser.setId(s);
        adminuser.setCreatedTime(new Date());
        adminuser.setUpdatedTime(new Date());
        int insert = adminUserMapper.insert(adminuser);
    }

    @Override
    public PagedResult queryAdminList(Integer page, Integer pageSize) {
        Example example = new Example(AdminUser.class);
        example.orderBy("createdTime").desc();
        PageHelper.startPage(page, pageSize);
        List<AdminUser> adminUsers = adminUserMapper.selectByExample(example);
        return commonUtil.setterPageResult(adminUsers, page);
    }


}
